﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Data;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
using Cinemon.MySqlConnect;
using Cinemon;


public partial class Staff_Edit : BaseClass
{
    protected void Page_Load(object sender, EventArgs e)
    {
		LoadPositions();

		if (!IsPostBack) {
			int id = Int32.Parse(Request.QueryString["id"]);
			Session["ID"] = id;

			using (DatabaseManager manager = new DatabaseManager()) {
				List<MySqlParameter> param = new List<MySqlParameter> {
				new MySqlParameter("Id",id)
			};
				DataTable table = manager.ExecuteQuery("GetStaff", param);
				txtName.Text = table.Rows[0]["Name"].ToString();
				txtSurname.Text = table.Rows[0]["Surname"].ToString();
				string datestring = table.Rows[0]["HireDate"].ToString();
				txtHireDate.Text = string.Format("{0:yyyy/MM/dd}", DateTime.Parse(datestring));
				ddlPosition.SelectedValue = table.Rows[0]["Position"].ToString();
			}
		}
    }

	protected void editStaff_Click(object sender, EventArgs e) {
		using (DatabaseManager manager = new DatabaseManager()) {
			List<MySqlParameter> param = new List<MySqlParameter> {
				new MySqlParameter("Id", int.Parse(Session["ID"].ToString())),
				new MySqlParameter("staff_Name", txtName.Text),
				new MySqlParameter("staff_Surname", txtSurname.Text),
				new MySqlParameter("staff_HireDate", txtHireDate.Text),
				new MySqlParameter("staff_Position", ddlPosition.SelectedValue.ToString()),
			};
			manager.ExecuteNonQuery("updateStaff", param);
		}
		Response.Redirect("view.aspx");
	}

	protected void LoadPositions() {
		using (DatabaseManager manager = new DatabaseManager()) {
			ddlPosition.DataSource = manager.ExecuteQuery("GetAllRoles");
			ddlPosition.DataTextField = "Name";
			ddlPosition.DataValueField = "Id";
			ddlPosition.DataBind();
		}
	}
}